package cn.codergege.demo.mock

import cn.codergege.demo.config.AppConfig
import cn.codergege.demo.config.DetachedJavaConfig
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification

@ContextConfiguration(classes = [DetachedJavaConfig.class, AppConfig.class])
class PublisherSpec extends Specification {
    @Autowired
    Publisher publisher
    @Autowired
    Subscriber subscriber
    Subscriber subscriber2 = Mock()

    def setup() {
        publisher.subscribers << subscriber
        publisher.subscribers << subscriber2
    }

    def "should send messages to all subscribers"() {
        when:
        publisher.send("hello")

        then:
        1 * subscriber.receive("hello1")
        1 * subscriber2.receive("hello")
    }
}
